 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : CdutHelperAndroid
 * Package  : net.solosky.cduthelper.android.adapter
 * File     : FunctionMenuListAdapter.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2011-5-20
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.view;

import java.util.List;

import net.solosky.easyavatar.R;
import net.solosky.easyavatar.bean.AuthInfo;
import net.solosky.easyavatar.bean.ProviderMeta;
import net.solosky.easyavatar.bean.ProviderMeta.AuthType;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 *
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class ProviderListAdapter extends SimpleListAdapter<ProviderMeta> {
	public ProviderListAdapter(Context context, List<ProviderMeta> dataList) {
		super(context, dataList);
	}

	/* (non-Javadoc)
	 * @see net.solosky.cduthelper.android.adapter.SimpleListAdapter#newView(int)
	 */
	@Override
	public ViewHolder newView(int position) {
		View view = inflate(R.layout.li_provider_item);
		ProviderMetaHolder holder = new ProviderMetaHolder();
		holder.contentView = view;
		holder.ivIcon = (ImageView)view.findViewById(R.id.iv_provider_icon);
		holder.tvTitle = (TextView)view.findViewById(R.id.tv_provider_title);
		holder.tvStatus = (TextView) view.findViewById(R.id.tv_providers_status_info);
		holder.blStatus = (View) view.findViewById(R.id.bl_provider_status);
		return holder;
	}

	/* (non-Javadoc)
	 * @see net.solosky.cduthelper.android.adapter.SimpleListAdapter#fillView(net.solosky.cduthelper.android.adapter.SimpleListAdapter.ViewHolder, int)
	 */
	@Override
	public void fillView(ViewHolder viewHolder, int position) {
		ProviderMeta meta = (ProviderMeta) getItem(position);
		ProviderMetaHolder holder = (ProviderMetaHolder) viewHolder;
		holder.ivIcon.setBackgroundResource(
				getContext().getResources().getIdentifier(
						meta.getIconSmall(), "drawable", getContext().getPackageName()
				));
		holder.tvTitle.setText(meta.getTitle());
		AuthInfo authInfo = meta.getAuthInfo();
		if(authInfo!=null){
			if(meta.getAuthType()==AuthType.OAUTH && authInfo.isAuthorized()){
				holder.blStatus.setVisibility(View.VISIBLE);
				holder.tvStatus.setText(getContext().getResources().getString(R.string.authronized));
			}else if(meta.getAuthType()==AuthType.WEB && authInfo.isAutoLogin()){
				holder.blStatus.setVisibility(View.VISIBLE);
				holder.tvStatus.setText(getContext().getResources().getString(R.string.autologined));
			}else{
				holder.blStatus.setVisibility(View.INVISIBLE);
			}
		}else{
			holder.blStatus.setVisibility(View.INVISIBLE);
		}
	}
	
	public final class ProviderMetaHolder extends ViewHolder{
		public ImageView ivIcon;
		public TextView tvTitle;
		public View		 blStatus;
		public TextView tvStatus;
	}

}
